/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PropertyRental.ejb.beans;

import PropertyRental.ejb.persistence.Property;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Bast
 */
@Stateless
public class PropertyFacade extends AbstractFacade<Property> {
    @PersistenceContext(unitName = "PropertyRental-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public PropertyFacade() {
        super(Property.class);
    }

public Property addProperty(String type, String adress, int numberBedroom, int numberBathroom,
            int numberOtherRoom, double rent, String location) {
        Property property= new Property(type, adress, numberBedroom, numberBathroom,
             numberOtherRoom, rent, location);
        try {
            create(property);
        } catch (Exception e) {
            return null ;
        }
        return property;
    }

public List<Property> findUsingCriteria(String type, int numberBedroom, int numberBathroom,
        int numberOtherRoom, double minrent, double maxrent, String location) {
        try {
            String SQL = "SELECT u FROM Property u WHERE ";

            if(!type.equals(""))
                SQL = SQL + "u.type = :PropertyType AND ";
            if(numberBedroom >= 0)
                SQL = SQL + "u.numberBedroom = :PropertyNBed AND ";
            if(numberBathroom >= 0)
                SQL = SQL + "u.numberBathroom = :PropertyNBath AND ";
            if(numberOtherRoom >= 0)
                SQL = SQL + "u.numberOtherRoom = :PropertyNOthe AND ";
            if(!location.equals(""))
                SQL = SQL + "u.location = :PropertyLocation AND ";
            if(minrent >= 0 && maxrent > 0)
                SQL = SQL + "u.rent BETWEEN :PropertyMin AND :PropertyMax";
            else
                SQL = SQL + " 1=1";

            Query query = em.createQuery(SQL);
            
            if(!type.equals(""))
                query.setParameter("PropertyType",type);
            if(numberBedroom >= 0)
                query.setParameter("PropertyNBed", numberBedroom);
            if(numberBathroom >= 0)
                query.setParameter("PropertyNBath", numberBathroom);
            if(numberOtherRoom >= 0)
                query.setParameter("PropertyNOthe", numberOtherRoom);
            if(minrent >= 0)
                query.setParameter("PropertyMin", minrent);
            if(maxrent > 0)
                query.setParameter("PropertyMax", maxrent);
            if(!location.equals(""))
                query.setParameter("PropertyLocation", location);

            List resultList = query.getResultList();
            return resultList;
        } catch (Exception e) {
        }
        return null;
    }

public boolean deleteProperty(Long id) {
        try {
            //delete the property using the id here
        } catch (Exception e) {
            return false ;
        }
        return true;
    }

public boolean updateProperty(Long id, String type, String adress, int numberBedroom, int numberBathroom,
            int numberOtherRoom, double rent, String location) {
        try {
            //write here the SQL request to update the Property
        } catch (Exception e) {
            return false ;
        }
        return true;
    }
}
